package com.post799.soundapp;

import com.bappi.utils.AlertUtils;
import com.bappi.utils.Constants;
import com.bappi.utils.Utils;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.Dialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class AlarmReciverActivity extends Activity {

	private MediaPlayer mAlarmMediaPlayer;
	private PowerManager.WakeLock mWakeLock;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);

			sp = PreferenceManager.getDefaultSharedPreferences(this);

			PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
			mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "WAKE UP");
			mWakeLock.acquire();

			String alramTone = sp.getString(Constants.KEY_ALARM_FILE, null);
			boolean enableSnooze = sp.getBoolean(
					Constants.KEY_ALARM_ENABLE_SNOOZE, false);

			if (!enableSnooze) {
				AlertUtils.showCustomAlert(this,
						getString(R.string.alarm_activated), null,
						getString(R.string.dismiss),
						new AlertUtils.OnButtonClickListener() {
							@Override
							public void onClick(View v, Dialog dialog) {
								try {
									dialog.dismiss();
									sp.edit()
											.putLong(Constants.KEY_ALARM_TIME,
													0).commit();
									mAlarmMediaPlayer.stop();
									finish();
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						});
			} else {
				AlertUtils.showCustomAlert(this,
						getString(R.string.alarm_activated), null,
						getString(R.string.dismiss),
						getString(R.string.snooze),
						new AlertUtils.OnButtonClickListener() {
							@Override
							public void onClick(View v, Dialog dialog) {
								try {
									dialog.dismiss();
									sp.edit()
											.putLong(Constants.KEY_ALARM_TIME,
													0).commit();
									mAlarmMediaPlayer.stop();
									finish();
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						}, new AlertUtils.OnButtonClickListener() {
							@Override
							public void onClick(View v, Dialog dialog) {
								try {
									dialog.dismiss();
									Intent intent = new Intent(
											AlarmReciverActivity.this,
											AlarmReciverActivity.class);
									PendingIntent pendingIntent = PendingIntent
											.getActivity(
													AlarmReciverActivity.this,
													12345,
													intent,
													PendingIntent.FLAG_CANCEL_CURRENT);
									AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
									long alarmTime = System.currentTimeMillis()
											+ ((5 * 60) * 1000);
									am.set(AlarmManager.RTC_WAKEUP, alarmTime,
											pendingIntent);
									sp.edit()
											.putLong(Constants.KEY_ALARM_TIME,
													alarmTime).commit();
									mAlarmMediaPlayer.stop();
									finish();
								} catch (Exception e) {
									e.printStackTrace();
								}
							}
						});
			}
			mAlarmMediaPlayer = MediaPlayer.create(this,
					Utils.getResourceID(alramTone, "raw", this));
			mAlarmMediaPlayer.start();
			mAlarmMediaPlayer.setLooping(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mWakeLock.release();
	}

	// @Override
	// public void onClick(View v) {
	// try {
	// switch (v.getId()) {
	// case R.id.dismissAlarm:
	// mAlarmMediaPlayer.stop();
	// finish();
	// break;
	// case R.id.snoozeAlarm:
	// Intent intent = new Intent(AlarmReciverActivity.this,
	// AlarmReciverActivity.class);
	// PendingIntent pendingIntent = PendingIntent.getActivity(
	// AlarmReciverActivity.this, 12345, intent,
	// PendingIntent.FLAG_CANCEL_CURRENT);
	// AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
	// long alarmTime = System.currentTimeMillis()
	// + ((5 * 60) * 1000);
	// am.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
	// sp.edit().putLong(Constants.KEY_ALARM_TIME,alarmTime).commit();
	// mAlarmMediaPlayer.stop();
	// finish();
	// break;
	//
	// default:
	// break;
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	//
	// }

}
